package com.github.cakin.shiro.chapter22.web.taglib;

import com.github.cakin.shiro.chapter22.entity.Organization;
import com.github.cakin.shiro.chapter22.entity.Resource;
import com.github.cakin.shiro.chapter22.entity.Role;
import com.github.cakin.shiro.chapter22.service.OrganizationService;
import com.github.cakin.shiro.chapter22.service.ResourceService;
import com.github.cakin.shiro.chapter22.service.RoleService;
import com.github.cakin.shiro.spring.SpringUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collection;

/**
 * @className: Functions
 * @description: 自定义标签类
 * @date: 2020/5/26
 * @author: cakin
 */
public class Functions {
    /**
     * 功能描述：iterable 是否包括 element
     *
     * @author cakin
     * @date 2020/5/26
     * @param iterable 迭代器对象
     * @param element 元素
     * @return boolean 元素是否包含在迭代器中
     * @description:
     */
    public static boolean in( Iterable iterable, Object element ) {
        if (iterable == null) {
            return false;
        }
        return CollectionUtils.contains(iterable.iterator(), element);
    }

    /**
     * 功能描述：根据组织ID获得组织名称
     *
     * @param organizationId 组织id
     * @return String 组织名称
     * @author cakin
     * @date 2020/5/26
     */
    public static String organizationName( Long organizationId ) {
        Organization organization = getOrganizationService().findOne(organizationId);
        if (organization == null) {
            return "";
        }
        return organization.getName();
    }

    /**
     * 功能描述：根据组织ID列表获得组织名称列表
     *
     * @param organizationIds 组织id列表
     * @return String 组织名称
     * @author cakin
     * @date 2020/5/26
     */
    public static String organizationNames( Collection<Long> organizationIds ) {
        if (CollectionUtils.isEmpty(organizationIds)) {
            return "";
        }

        StringBuilder s = new StringBuilder();
        for (Long organizationId : organizationIds) {
            Organization organization = getOrganizationService().findOne(organizationId);
            if (organization == null) {
                return "";
            }
            s.append(organization.getName());
            s.append(",");
        }

        if (s.length() > 0) {
            s.deleteCharAt(s.length() - 1);
        }

        return s.toString();
    }

    /**
     * 功能描述：根据角色ID获得角色名称
     *
     * @param roleId 角色id
     * @return String 角色名称
     * @author cakin
     * @date 2020/5/26
     */
    public static String roleName( Long roleId ) {
        Role role = getRoleService().findOne(roleId);
        if (role == null) {
            return "";
        }
        return role.getDescription();
    }

    /**
     * 功能描述：根据角色ID集合获得角色名称集合
     *
     * @param roleIds 角色id集合
     * @return String 角色名称
     * @author cakin
     * @date 2020/5/26
     */
    public static String roleNames( Collection<Long> roleIds ) {
        if (CollectionUtils.isEmpty(roleIds)) {
            return "";
        }

        StringBuilder s = new StringBuilder();
        for (Long roleId : roleIds) {
            Role role = getRoleService().findOne(roleId);
            if (role == null) {
                return "";
            }
            s.append(role.getDescription());
            s.append(",");
        }

        if (s.length() > 0) {
            s.deleteCharAt(s.length() - 1);
        }

        return s.toString();
    }

    /**
     * 功能描述：根据resourceId获得资源名称
     *
     * @param resourceId 资源id
     * @return String 资源名称
     * @author cakin
     * @date 2020/5/26
     */
    public static String resourceName( Long resourceId ) {
        Resource resource = getResourceService().findOne(resourceId);
        if (resource == null) {
            return "";
        }
        return resource.getName();
    }

    /**
     * 功能描述：根据resourceId集合获得资源名称列表
     *
     * @param resourceIds 资源id集合
     * @return String 资源名称集合
     * @author cakin
     * @date 2020/5/26
     */
    public static String resourceNames( Collection<Long> resourceIds ) {
        if (CollectionUtils.isEmpty(resourceIds)) {
            return "";
        }

        StringBuilder s = new StringBuilder();
        for (Long resourceId : resourceIds) {
            Resource resource = getResourceService().findOne(resourceId);
            if (resource == null) {
                return "";
            }
            s.append(resource.getName());
            s.append(",");
        }

        if (s.length() > 0) {
            s.deleteCharAt(s.length() - 1);
        }

        return s.toString();
    }

    /**
     * 组织服务
     */
    private static OrganizationService organizationService;
    /**
     * 角色服务
     */
    private static RoleService roleService;
    /**
     * 资源服务
     */
    private static ResourceService resourceService;

    /**
     * 功能描述：通过Spring的工具类获得组织服务
     *
     * @return OrganizationService 组织服务
     * @author cakin
     * @date 2020/5/26
     */
    public static OrganizationService getOrganizationService() {
        if (organizationService == null) {
            organizationService = SpringUtils.getBean(OrganizationService.class);
        }
        return organizationService;
    }

    /**
     * 功能描述：通过Spring的工具类获得角色服务
     *
     * @return RoleService 角色服务
     * @author cakin
     * @date 2020/5/26
     */
    public static RoleService getRoleService() {
        if (roleService == null) {
            roleService = SpringUtils.getBean(RoleService.class);
        }
        return roleService;
    }

    /**
     * 功能描述：通过Spring的工具类获得资源服务
     *
     * @return ResourceService 资源服务
     * @author cakin
     * @date 2020/5/26
     */
    public static ResourceService getResourceService() {
        if (resourceService == null) {
            resourceService = SpringUtils.getBean(ResourceService.class);
        }
        return resourceService;
    }
}

